import { Square } from './Square'
import { IPoint, Shape } from './types'

export class SquareGroup {
  private _squares: ReadonlyArray<Square> = []

  get squares(): ReadonlyArray<Square> {
    return this._squares
  }

  get shape() {
    return this._shape
  }

  get centerPoint(): IPoint {
    return this._centerPoint
  }

  set centerPoint(val: IPoint) {
    this._centerPoint = val
    this.setSquarePoints()
  }

  private setSquarePoints() {
    this._shape.forEach((p, i) => {
      this.squares[i].point = {
        x: this._centerPoint.x + p.x,
        y: this._centerPoint.y + p.y
      }
    })
  }

  constructor(private _shape: Shape, private _centerPoint: IPoint, private _color: string) {
    const arr: Square[] = []
    this._shape.forEach(item => {
      const square = new Square()
      square.color = this._color
      arr.push(square)
    })
    this._squares = arr
    this.setSquarePoints()
  }

  protected isClock = true

  afterRotateShape(): Shape {
    if (this.isClock) {
      return this._shape.map(item => ({
        x: -item.y,
        y: item.x
      }))
    } else {
      return this._shape.map(item => ({
        x: item.y,
        y: -item.x
      }))
    }
  }

  rotate() {
    const newShape = this.afterRotateShape()
    this._shape = newShape
    this.setSquarePoints()
  }
}
